// xc/components/widgets/xccountdown.js
Component({
  options: {
    addGlobalClass: true,
    multipleSlots: true
  },
  /**
   * 组件的属性列表
   */
  properties: {
    mode:{
      type: String,
      value: 'none'  //time //next // none
    },
    time: {
      type: Number,
      observer: function (newVal, oldVal) {

        this.start(newVal)

        // this.setData({ntime})

      }
    }

  },

  /**
   * 组件的初始数据
   */
  data: {
    ntime: 0,
    timeid: -1,
    isstart:false

  },

  /**
   * 组件的方法列表
   */
  methods: {

    cancelcountdown() {
      this.restcountdown()

      this.setData({mode:'next'})
    },
    restcountdown() {
      this.setData({mode:'none'})
      if (this.data.timeid) {
        clearTimeout(this.data.timeid)
        this.data.timeid = -1
        this.data.isstart=false
      }
 
    },
    start(ntime) {
      this.restcountdown();
      this.data.isstart=true;
      this.setData({ ntime: ntime, isstart: 1,mode:"time" })

      this.tick();
    },
    filshcountdown:function(){
      console.log("立即执行")
      this.restcountdown();
      this.triggerEvent("finish", {})
    },
    tick: function () {
      var that = this
      var ntime = that.data.ntime
      if ( this.data.isstart&&ntime > 0) {

        this.data.timeid = setTimeout(function () {
          ntime--
          var updatemodel = { ntime }
          if (ntime > 0) {

            that.tick();
            that.triggerEvent("change", ntime)
          }
          else {
            that.triggerEvent("finish", ntime)
            // updatemodel[""]
            // this.$emit('finish');
          }
          that.setData({ ntime })
        }, 1000)
      }
    }
  }
})
